<template>
  <div class="p20 bg-white console">
    <div class="mt10 mb20">
      <el-button
        class="theme-btn"
        @click="handleAdd"
        v-if="permitIds.includes('business_apart_detail_add')"
        >新增房型</el-button
      >
      <el-button @click="handleSetFeeStandard">收费标准设置</el-button>
      <span class="ml10">
        <i class="label">价格排序：</i>
        <el-select
          v-model="sort"
          @change="handleUpdate"
          clearable
          class="small-select"
        >
          <el-option label="升序" value="1"></el-option>
          <el-option label="降序" value="2"></el-option>
        </el-select>
      </span>
      <Search :placeholder="placeholder" @update="handleUpdate"></Search>
    </div>
    <ApartmentDetailDialog
      :title="title"
      :loading="loading"
      @close="hanleClose"
      @update="handleUpdate"
      :addDialog="addDialog"
      :editDialog="editDialog"
      :detailDialog="detailDialog"
      :apartmentDetailForm="apartmentDetailForm"
    ></ApartmentDetailDialog>
    <el-table
      v-loading="loading"
      border
      stripe
      element-loading-text="拼命加载中"
      :data="tableData"
    >
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="roomType" label="公寓房型"></el-table-column>
      <el-table-column prop="area" label="面积(㎡)"></el-table-column>
      <el-table-column prop="price" label="价格(元/月)"></el-table-column>
      <el-table-column prop="propertyType" label="房屋图片" width="350">
        <template slot-scope="scope" v-if="scope.row.photos">
          <img
            class="detail-photo inline-block"
            :src="
              scope.row.photos.split(',')[0]
                ? scope.row.photos.split(',')[0]
                : ''
            "
            alt
          />
          <img
            class="detail-photo inline-block ml10"
            :src="
              scope.row.photos.split(',')[1]
                ? scope.row.photos.split(',')[1]
                : ''
            "
            alt
          />
          <img
            class="detail-photo inline-block ml10"
            :src="
              scope.row.photos.split(',')[2]
                ? scope.row.photos.split(',')[2]
                : ''
            "
            alt
          />
        </template>
      </el-table-column>
      <el-table-column prop="remaining" label="剩余(套)"></el-table-column>
      <el-table-column prop="count" label="总数(套)"></el-table-column>
      <el-table-column prop="createDate" label="创建时间"></el-table-column>
      <el-table-column prop="operation" label="操作" width="350">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            class="theme-btn"
            @click="handleEdit(scope.row)"
            v-if="permitIds.includes('business_apart_detail_update')"
            >修改</el-button
          >
          <el-button
            size="mini"
            @click="handleDelete(scope.row.id)"
            v-if="permitIds.includes('business_apart_detail_delete')"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :pageTotal="pageTotal"
      @paginationChange="handleUpdate"
    ></Pagination>
    <FeeStandardDialog
      :feeStandardDialog="feeStandardDialog"
      @close="hanleClose"
    ></FeeStandardDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Search from "@/components/search";
import Pagination from "@/components/pagination";
import ApartmentDetailDialog from "./components/apartment_detail_dialog";
import FeeStandardDialog from "./components/fee_standard_dialog";
import { getAllApartmentDetail, deleteApartmentDetail } from "@/apis/business";

export default {
  name: "House_type",
  data() {
    return {
      sort: "",
      title: "",
      address: "",
      pageTotal: 0,
      tableData: [],
      addDialog: false,
      editDialog: false,
      detailDialog: false,
      apartmentDetailForm: {},
      feeStandardDialog: false,
      placeholder: "输入公寓房型搜索"
    };
  },
  created() {
    this.$store.dispatch("load/setLoading", true);
    this.$store.dispatch("pagination/resetPageNum");
    this.getApartmentDetails(this.pageSize, this.pageNum);
  },
  computed: {
    ...mapGetters(["pageSize", "pageNum", "queryStr", "permitIds", "loading"])
  },
  methods: {
    async getApartmentDetails(pageSize, pageNum) {
      const { object } = await getAllApartmentDetail({
        pageNum,
        pageSize,
        sort: this.sort,
        queryStr: this.queryStr,
        apartmentId: +sessionStorage.getItem("apartmentId")
      });

      this.pageTotal = object.total;
      this.tableData = object ? object.records : [];
      this.$store.dispatch("load/setLoading", false);
    },
    handleAdd() {
      this.addDialog = !this.addDialog;
      this.title = "新增房型";
    },
    hanleClose() {
      this.addDialog = false;
      this.editDialog = false;
      this.detailDialog = false;
      this.feeStandardDialog = false;
      this.$store.dispatch("load/setLoading", false);
    },
    handleUpdate() {
      this.getApartmentDetails(this.pageSize, this.pageNum);
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条, 是否继续?", "提示", {
        type: "warning",
        cancelButtonText: "取消",
        confirmButtonText: "确定"
      })
        .then(() => {
          this.$store.dispatch("load/setLoading", true);
          // deleteApartmentDetail({ apartmentDetailsId: id }).then(res => {
          //   this.$notify({
          //     title: res.msg,
          //     type: "success"
          //   });
          //   this.handleUpdate();
          //   this.$store.dispatch("load/setLoading", false);
          // });
          this.$notify({
            title: "操作成功！",
            type: "success"
          });
          this.handleUpdate();
          this.$store.dispatch("load/setLoading", false);
        })
        .catch(() => {
          this.$message("已取消删除");
        });
    },
    handleSetFeeStandard() {
      this.feeStandardDialog = !this.feeStandardDialog;
    },
    handleEdit(item) {
      this.title = "修改房型";
      this.apartmentDetailForm = item;
      this.editDialog = !this.editDialog;
    },
    handleDetail(item) {
      this.title = "房型详情";
      this.apartmentDetailForm = item;
      this.detailDialog = !this.detailDialog;
    }
  },
  components: {
    Search,
    Pagination,
    FeeStandardDialog,
    ApartmentDetailDialog
  }
};
</script>

<style scoped lang="less"></style>
